/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.web.context.response;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @description: JsonResult
 * @author: zzh
 * @date: 2021/9/6 17:16
 */
@Data
@Accessors(chain = true)
public class JsonResult<T> implements Serializable {

  private Integer code;

  private String message;

  private T data;

  private JsonResult(Integer code, String message, T data) {
    this.code = code;
    this.message = message;
    this.data = data;
  }

  public static JsonResult success(Object data){
    return new JsonResult(
        ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMessage(), data);
  }

  public static JsonResult success(String message,Object data){
    return new JsonResult(ResponseCodeEnum.SUCCESS.getCode(), message, data);
  }

  public static JsonResult success(Integer code,String message,Object data){
    return new JsonResult(code, message, data);
  }

  public static JsonResult failure(){
    return new JsonResult(
        ResponseCodeEnum.FAILURE.getCode(), ResponseCodeEnum.FAILURE.getMessage(), null);
  }

  public static JsonResult failure(String message){
    return new JsonResult(ResponseCodeEnum.FAILURE.getCode(), message, null);
  }

  public static JsonResult failure(Integer code,String message){
    return new JsonResult(code, message, null);
  }

  public static JsonResult unauthorized(){
    return new JsonResult(
        ResponseCodeEnum.UNAUTHORIZED.getCode(), ResponseCodeEnum.UNAUTHORIZED.getMessage(),null);
  }

  public static JsonResult unauthorized(String message){
    return new JsonResult(
        ResponseCodeEnum.UNAUTHORIZED.getCode(), message,null);
  }
}
